<?php
namespace Admin\Controller;
use Think\Controller;
/*
后台 角色管理、权限分类、权限管理
 */
class RbacController extends CommonController {
    
  //管理员列表------------------------------------------------------
  public function admin_list()
  {

    $sql="select t1.*,t3.title from admin t1 inner join auth_group_access t2 on t1.id=t2.uid inner join auth_group t3 on t2.group_id=t3.id ";
    $data=M()->query($sql);
    // var_dump($data);
    $this->assign('data',$data);
    $this->display();
  }
  //管理管增加模板
  public function admin_listadd()
  {
    $sql = "select title,id from auth_group";
    $data=M()->query($sql);
    // var_dump($data);
    $this->assign('data',$data);
    $this->display();
  }
  //管理增加方法
  public function listadd()
  {
    $data=I('post.');
    // var_dump($data);
    $data['create_time']=time();
    $data['password']=MD5($data['pass']);
    $phone=$data['phone'];
    $username=$data['username'];

    $role['group_id']=$data['role'];

    if($data['pass']==$data['repass']){

      $sql="select phone from admin where phone='$phone'";
      if(!M()->query($sql)){

        $sql="select username from admin where username='$username'";
        if (!M()->query($sql)) {
          
          $res=M('admin')->add($data);
          // var_dump($res);exit;
           if ($res) {

            $role['uid']=$res['id'];

              if (M('auth_group_access')->add($role)) {

                Reajax(0,"添加成功");
              }
 
            }else{
              Reajax(1,"添加失败");
            } 

        }else{
            Reajax(1,"用户名已存在");
        }

      }else{
        Reajax(1,"电话号码已存在");
      }

    }else{
    Reajax(1,"两次密码不一样，请重新输入");
    };

  }
  //管理停用
  public function admin_stops()
  {
      $uid=I('post.id');
      // var_dump($uid);
      $data['status']=1;
      $data['id']=$uid;
      if (M('admin')->save($data)) {
        Reajax(0,"停用成功");
      }else{
        Reajax(1,"停用失败");
      }
  }
  //管理启用
  public function admin_starts()
  {
      $uid=I('post.id');
      $data['status']=0;
      $data['id']=$uid;
      if (M('admin')->save($data)) {
        Reajax(0,"启用成功");
      }else{
        Reajax(1,"启用失败");
      }
  }
  //编辑模板——展现
  public function list_edit()
  {
    $id = I('get.id');

    $sql = "select t1.username,t1.phone,t2.uid,t3.id from admin t1 inner join auth_group_access t2 on t1.id=t2.uid inner join auth_group t3 on t2.group_id=t3.id where t1.id='$id'";
    $data = M()->query($sql);
    $this->assign('data',current($data));
    // var_dump($data);
    $sql = "select title,id from auth_group";
    $res=M()->query($sql);
    // var_dump($res);
    $this->assign('res',$res);
    $this->display();
  }
  //编辑方法
  public function list_save()
  {
    $data = I('post.');
    // var_dump($data);
    $id=$data['id'];
    $data['password'] = md5($data['pass']);
    $username = $data['username'];
    $phone = $data['phone'];
    // var_dump($phone);
    $sql = "select * from admin where phone='$phone' and id<>'$id'";
    $pho = M()->query($sql);
    // var_dump($pho);
    if ($pho) {
       Reajax(1,"电话号码已存在");
    }
    $sql = "select * from admin where username='$username' and id<>'$id'";
    $name = M()->query($sql);
    if ($name) {
     Reajax(1,"用户名已存在");
    }

    $save = M('admin')->save($data);
    if ($save) {

      $num['group_id'] = $data['role'];
      M('auth_group_access')->where("uid='$id'")->save($num);

      Reajax(0,"编辑成功");
    }else{
      Reajax(1,"编辑失败");
    }


  }
  //管理员列表删除
  public function admin_del()
  {

    $id = I('post.id');
    $data['status']='1';
    $res = M('admin')->where("id='$id'")->save($data);
     
      if ($res) {
        Reajax(0,"删除成功");
      }else{
        Reajax(1,"删除失败");
      }
  }
//------------------------分类管理-----------------------------------------
 //引模板
  public function cate()
  {
    $data = M('cate')->select();
    $this->assign('data',$data);
    $this->display();
  }
 //分类添加
 public function cate_add()
 {
   $data = I('post.');
   $name = $data['cname'];
   // var_dump($name);
   $res = M('cate')->where("cname='$name'")->select();
   // var_dump($res);
   if ($res) {
      Reajax(1,"分类名已存在");
   }
   if(M('cate')->add($data)){
    Reajax(0,"添加分类成功");
   }else{
    Reajax(1,"添加失败");
   }

 }








//----------------------------权限管理----------------------------------------------------
    //权限管理引模板
    public function rbac(){
      // echo "我是Rbac";
      $data=M('auth_rule')->select();
      $arr = [];
      //去重
      foreach ($data as $row) {
        $arr[] = $row['cname'];
      }
      $this->assign('rule',array_unique($arr));

      $this->assign('data',$data);
      $this->display();

    }
    //权限编辑
    public function rbacedit()
    {
      $data = M('auth_rule')->find(I('get.id'));
      // var_dump($data);
      $this->assign('data',$data);
      $this->display();
    }
    //权限编辑方法
    public function rbac_edit()
    {
      $data=I('post.');
      // var_dump($data);
      $id=I('post.id');

      if (M('auth_rule')->where("id='$id'")->save($data)) {
         
         Reajax(0,"修改完成");

      }else{
        Reajax(1,"修改失败");
      }
    }
    //权限删除方法(有两个表要删除)
    public function rbac_del()
    {
      $uid=I('post.uid');

      $res=M('auth_rule')->where("id='$uid'")->delete();//删除auth_rule表里面ID=？的某项

      $auth_group = M('auth_group')->select();

      foreach ($auth_group as $row) {

          $rules = explode(',', $row['rules']);//去掉$row['rules']里面,号：得到一个数组

          if(in_array($uid, $rules)){
            //如果$uid存在数组$rules里面  
            unset($rules[array_search($uid,$rules)]);//去掉存在里面的那一项在组一个新的数组

            $row['rules'] = implode(',', $rules);//把数组用,隔开组成新的字符串

            M('auth_group')->save($row);//更改表里面的值
          }
      }

      if ($res) {
        Reajax(0,"删除成功");
      }else{
        Reajax(1,"没有删除");
      }
    }
    //批量删除
    public function rbac_delall()
    {
        $uids=I('post.uids');
        $uids=rtrim($uids,',');
        // var_dump($uids);
        $id=explode(',', $uids);

        $sql="delete from auth_rule where id in (".$uids.")";//这个是真的删除
        $res=M('auth_rule')->execute($sql);

        $auth_group = M('auth_group')->select();

        foreach ($id as $v) {

          foreach ($auth_group as $row) {

              $rules = explode(',', $row['rules']);//去掉$row['rules']里面,号：得到一个数组

              if(in_array($v, $rules)){
                //如果$uid存在数组$rules里面  
                unset($rules[array_search($v,$rules)]);//去掉存在里面的那一项在组一个新的数组

                $row['rules'] = implode(',', $rules);//把数组用,隔开组成新的字符串

                M('auth_group')->save($row);//更改表里面的值
              }
          }
        }

        if ($res) {
          Reajax(0,"批量删除成功");
        }else{
          Reajax(1,"批量删除失败");
        }
    }
    //权限添加方法
    public function rbac_add()
    {
          $data=I('post.');
          // var_dump(999);

      if (M('auth_rule')->add($data)) {
        
          Reajax(0,"添加成功");
          
      }else{
        Reajax(1,"请你填写增加内容");

      };
      
     }    
     //角色管理模板-----------------------------------------------------------------
     public function role()
     {

      $sql="select * from auth_group";
      $res=M()->query($sql);

      foreach ($res as $k => $v) {
                             //GROUP_CONCAT直接把(2,3,4)组成(会员删除,会员等级,会员积分)
        $res[$k]['rulestitle']=current(M()->query("select GROUP_CONCAT(title) title from auth_rule where id in (".$v['rules'].")"))['title'];
      }
      // var_dump($res);
      $this->assign('data',$res);
      $this->display();
     }
     //角色权利添加
     public function roleadd()
     {
        
        $res=M('auth_rule')->select();
        
        $arr = [];
        foreach ($res as $row) {

          $arr[$row['cname']][]=$row;

        }
        // var_dump($arr);
        $this->assign('rules',$arr);

        $this->display();
     }
     //角色添加方法
     public function rbacaadd()
     {
       $data = I('post.');
       // var_dump($data);
       $id = $data['id'];
       // var_dump($id);
       $data['rules'] = implode(',', $id);
       // var_dump($data['rules']);
       
       $res=M('auth_group')->add($data);

        if ($res) {

            Reajax(0,"增加成功");
        }else{
            Reajax(1,"增加失败");
        }
     }
     //删除角色管理
     public function role_dels()
     {

       $id=I('post.id');
       
       $data = M('auth_group')->find("id='$id'");
       
       // var_dump($data);exit;
       $sql="delete from auth_group where id='$id'";

      $res=M()->execute($sql);
      
      if ($res) {

          $this->log("删除了一个角色管理员");

        Reajax(0,"批量删除成功");
        

      }else{
        Reajax(1,"批量删除失败");
      }

     }
     //角色编辑模板
     public function role_edit()
     {
        $id = I('get.id');
        // var_dump($id);
        $sql = "select * from auth_group where id='$id'";
        $res = M()->query($sql);  
        $this->assign('res',current($res));
        // var_dump($res);
        $sqll = "select * from auth_rule ";
        $ress = M()->query($sqll);
        $arr = [] ;
        foreach ($ress as $row) {
          $arr[$row['cname']][] = $row;
        }
        // var_dump($arr);
        $this->assign('rules',$arr);
        $this->display();
     }
     //角色编辑方法
     public function rbac_save()
     {
       $data = I('post.');
       $data['rules'] = implode(',', $data['rules']);

       if (M('auth_group')->save($data)) {

         Reajax(0,"编辑成功");
       }else{
        Reajax(1,"编辑失败");
       }
     }


}